﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSS_Simplex
{
    public partial class FormConstPop : Form
    {
        public FormConstPop(List<field> fields)
        {
            InitializeComponent();
            BuildControls(fields);
        }

        public event SearchContextChangedHandler searchgrid_TextChanged;

        private void BuildControls(List<field> fields) 
        {
            int top = 10;
            bool focused = false;

            foreach (field f in fields)
            {
                Label label = new Label();
                label.Text = f.FriendlyName +":";
                label.Top = top;
                label.AutoSize = true;
                this.Controls.Add(label);

                TextBox textbox = new TextBox();
                textbox.TextChanged += new EventHandler(textBox_TextChanged);
                textbox.Tag = f.Field;
                textbox.Top = top;
                textbox.Left = 78;
                textbox.Width = this.Width - 100;
                if (!focused)
                {
                    textbox.Focus(); //the first control focused
                    focused = true;
                }
                top += 35;
                this.Controls.Add(textbox);
            }
            this.Height = top + 30;
        }

        
        private List<field> GetFilterValues()
        {
            List<field> fields = new List<field>();

            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                {
                    field f = new field();
                    f.Field = ctrl.Tag.ToString();
                    f.Value = ctrl.Text;
                    fields.Add(f);
                }
            }
            return fields;
        }
        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (searchgrid_TextChanged != null)
                searchgrid_TextChanged(GetFilterValues());
        }
    }
}
